<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>formData的使用</title>
  </head>

  <body>
    <!-- 创建普通的html表单 -->
    <form action="" id="form">
      <p>姓名:<input type="text" name="username"></p>
      <p>密码:<input type="text" name="password"></p>
      <p><input type="button" value="提交" id="btn"></p>
    </form>
  </body>
  <script>
    // 使用formData的方式获取表单
    let btn = document.querySelector("#btn");
    let form = document.querySelector("#form");
    btn.onclick = () => {
      let formData = new FormData(form);
      console.log(formData);
      // 发送ajax请求
      let xhr = new XMLHttpRequest();
      xhr.open('post', "http://127.0.0.1:3000/formData");
      xhr.send(formData);
      xhr.onload = () => {
        if (xhr.status == 200) {
          console.log(xhr.responseText);
        }
      }
    }
  </script>

</html>